// JavaScript Document
	var alarmEnable = false;
	var alarmHTML = "Alarm.html";
	var alarmID = 168;
	var alarmTime;
	// Wait for device API libraries to load
	//
	$(document).ready(onLoad);
	function onLoad() {
		setUIClock();
		setInterval("setUIClock()",1000);
		
		alarmTime = new Date(window.localStorage.getItem('alarmTime'));
		if(!window.localStorage.getItem('alarmTime'))
			alarmTime.setHours(7,30,0);
		
		setUIAlarmTime(alarmTime);
		
		$("#enable").click(function (){
				if(alarmEnable){
					setAlarmOFF();
				}else{
					setAlarmON(alarmTime);
				}			
			});		
		$("#alarmTime").click(pickTime);
					
		$("#clock").swipe({
  			swipeLeft:function(event, direction, distance, duration, fingerCount) {
   					$("body").pagecontainer("change", "index.html", { showLoadMsg: true, transition: 'slide', role:'page'});
			}}); 
	}
	function setUIClock(){
		var now = new Date();
		var strNow = String.format("{0}:{1}", String.padLeft( now.getHours(),2),String.padLeft( now.getMinutes(),2));
		$("#time").html(strNow);
		var strSec = String.padLeft(now.getSeconds(),2);
		$("#sec").html(strSec);
	}
	function setUIAlarmTime(alarmTime){
		$("#alarmTime").html(String.format("{0}:{1}",String.padLeft(alarmTime.getHours(),2), String.padLeft(alarmTime.getMinutes(),2)));
	}
	function pickTime()
	{
		var options = {
		date: alarmTime,
		mode: 'time',
		cancelButtonLabel: '取消'
		};
		datePicker.show(options, function(n_date){
			if(isNaN(n_date.getTime())) return;
			alarmTime = n_date;
			setUIAlarmTime(alarmTime);
			setAlarmON(alarmTime);
  		});
	}
	function setAlarmON(alarmTime){
			alarmEnable = true;
			$("#enable").html("ON");
			
			var today = new Date();
			alarmTime.setFullYear(today.getFullYear(), today.getMonth(), today.getDate());
			if( alarmTime < new Date()) {
				alarmTime.setDate(alarmTime.getDate()+1);
			}			
			
			var notificationObject = {title: "冠球鬧鐘", message:"點此關閉"};
	   		var alarmJsonObject = { alarmId:alarmID, timeInMillis:alarmTime.getTime(), htmlPath:alarmHTML, notification:notificationObject};
	   		navigator.alarmgap.saveAlarm(alarmJsonObject, showAlarmMessage, setAlarmError);	
			window.localStorage.setItem('alarmTime', alarmTime);		
	}
	var msecPerMinute = 1000 * 60;
	var msecPerHour = msecPerMinute * 60;
	function showAlarmMessage(){
		var interval = alarmTime.getTime() - new Date().getTime();
		var hours = Math.floor(interval / msecPerHour ) ?  Math.floor(interval / msecPerHour )+ "小時" : "" ;
		var mins =  (Math.floor(interval / msecPerMinute )) % 60 + "分鐘";
		alert(String.format("鬧鐘設定成功，將在{0}{1}後響起",hours,mins));
	}
	function setAlarmError(){}
	function setAlarmOFF(){
		alarmEnable = false;
		$("#enable").html("OFF");
		
		//navigator.alarmgap.stopAlarm(alarmID,Succ,Err);	//don't work
		var noAlarm = new Date(2100,1,1);					//設定100年後叫
		var notificationObject = {title: "冠球鬧鐘", message:"點此關閉"};
	   	var alarmJsonObject = { alarmId:alarmID, timeInMillis:noAlarm.getTime(), htmlPath:alarmHTML, notification:notificationObject};
	   	navigator.alarmgap.saveAlarm(alarmJsonObject);	
		window.localStorage.setItem('alarmTime', alarmTime);
	}